﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using yq.Enums.PurviewPage;

namespace RegisterPage
{
    public partial class FormMain : Form
    {

        public static string DbString ="";
        public IList<PurviewPage> List;

        public PurviewPage CurrPurview;

        public FormMain()
        {
            InitializeComponent();
        }

        
        public void info()
        {

          List = PurviewPageDAL.Select();

          if (cb_ShowClose.Checked)
          {
              List = List.Where(obj=>obj.IsOpen== yq.Enums.BooleanType.No || obj.Type !=  yq.Enums.PurviewPage.Type.Method ).ToList();
          }


          tvTable.Nodes.Clear();
          foreach (var info in List.Where(obj => obj.Type == yq.Enums.PurviewPage.Type.Area))
           {
               //外部循环,循环模块
               TreeNode mNode = new TreeNode();
               mNode.Text = info.Name;
               mNode.Name = info.Path;

               //循环下面的各表
               foreach (var table in List.Where(obj => obj.Type == yq.Enums.PurviewPage.Type.Controller && obj.Path.StartsWith(info.Path + "/")))
               {
                   TreeNode tNode = new TreeNode();
                   tNode.Text = table.Name;
                   tNode.Name = table.Path;

                   //循环下面的各表
                   foreach (var tab in List.Where(obj => obj.Type == yq.Enums.PurviewPage.Type.Method && obj.Path.StartsWith(table.Path + "/")))
                   {
                       TreeNode tNode1 = new TreeNode();
                       tNode1.Text = tab.Name;
                       tNode1.Name = tab.Path;
                       //添加到模块节点去
                       tNode.Nodes.Add(tNode1);
                   }


                   if ( tNode.Nodes.Count != 0)
                   {
                       //添加到模块节点去
                       mNode.Nodes.Add(tNode);
                   }
               }

               if ( mNode.Nodes.Count != 0)
               {
                   //添加到根节点去
                   tvTable.Nodes.Add(mNode);
               }
           }
        }

        private void but_Imp_Click(object sender, EventArgs e)
        {
            DbString = Txt_db.Text;
            Form1 fm = new Form1(Txt_db.Text);
            fm.ShowDialog();

        }

        private void but_select_db_Click(object sender, EventArgs e)
        {
            DbString = Txt_db.Text;
            info();
        }

        private void tvTable_AfterSelect(object sender, TreeViewEventArgs e)
        {
            CurrPurview = List.FirstOrDefault(obj => obj.Path == e.Node.Name);

           if (CurrPurview != null)
           {
               text_Name.Text = CurrPurview.Name;
               text_Detailed.Text = CurrPurview.Detailed;
               text_Path.Text = CurrPurview.Path;
               cb_Purview.SelectedIndex = CurrPurview.PurviewEnum.GetHashCode();
               cb_Type.SelectedIndex = CurrPurview.Type.GetHashCode();
           }
        }

        private void but_Save_Click(object sender, EventArgs e)
        {
            if (cb_Purview.SelectedIndex == 0)
            {
                MessageBox.Show("请选择一个权限");
            }
            CurrPurview.PurviewEnum = (PurviewEnum)cb_Purview.SelectedIndex;
            CurrPurview.Detailed=text_Detailed.Text;
            CurrPurview.Name = text_Name.Text;
            PurviewPageDAL.Update(CurrPurview);
        }

        private void cb_ShowClose_CheckedChanged(object sender, EventArgs e)
        {
            info();
        }
    }
}
